package cn.com.jit.oa.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.com.jit.oa.domain.Department;
import cn.com.jit.oa.domain.Role;
import cn.com.jit.oa.service.DepartmentService;
import cn.com.jit.oa.service.RoleService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class DepartmentAction extends ActionSupport{
	
	
	private Long id;
	private Long parentId;
	private Department parent;
	private Department department;
	private List<Department> departmentList;
	
	@Resource
	private DepartmentService departmentService;
	/**
	 * �б�
	 * @return
	 */
	public String list(){
		
		if(parentId == null){
			departmentList = departmentService.findTopList();
		}else{
			departmentList = departmentService.findChildrenList(parentId);
			parent = departmentService.getById(parentId);
		}
		
		return "list";
	}
	
	/**
	 * �h��
	 * @return
	 */
	public String delete(){
		parentId = departmentService.getById(id).getParent().getId();
		departmentService.delete(id);
		return "toList";
	}
	
	/**
	 * ������
	 * @return
	 */
	public String addUI(){
		department = new Department();
		department.setId(0l);
		
		departmentList = departmentService.findAll();
		
		return "saveUI";
	}
	
	/**
	 * ���
	 * @return
	 */
	public String add(){
		
		Department partment = departmentService.getById(parentId);
		department.setParent(partment);
		departmentService.save(department);
		
		return "toList";
	}
	
	/**
	 * �޸�
	 * @return
	 */
	public String edit(){
		
		Department partment = departmentService.getById(parentId);
		department.setParent(partment);
		departmentService.update(department);
		
		return "toList";
	}
	
	/**
	 * �޸����
	 * @return
	 */
	public String editUI(){
		
		departmentList = departmentService.findAll();
		department = departmentService.getById(id);
		if(department.getParent() != null){
			parentId = department.getParent().getId();
		}
		
		return "saveUI";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List<Department> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Department getParent() {
		return parent;
	}

	public void setParent(Department parent) {
		this.parent = parent;
	}

}
